package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageFreeAddOperatorActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageFreeSelectOperatorActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.bean.common.GetRoleBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageFreeOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class FreeOperatorListAdapter extends BaseAdapter {

    private Context mContext;
    private List<GetOperatorBean.OperatorBean> mOperatorList;
    private List<GetRoleBean.RoleBean> mRoleList;
    private LayoutInflater inflater;
    private String mPurchase, mSupplier, mRoleName, mRoleCode, mGroupName, mGroupCode;
    private InStorageFreeAddOperatorActivity.InStorageFreeAddOperatorListener mInStorageFreeAddOperatorListener;

    public FreeOperatorListAdapter(Context context, InStorageFreeOverviewBean.OverviewBean data, List<GetRoleBean.RoleBean> roleList, String roleName, String roleCode, String groupName, String groupCode, InStorageFreeAddOperatorActivity.InStorageFreeAddOperatorListener inStorageFreeAddOperatorListener) {
        mContext = context;
        mPurchase = data.getPurchase_order_code();
        mSupplier = data.getSupplier_name();
        mOperatorList = data.getUser_list() == null ? new ArrayList<>() : data.getUser_list();
        mRoleList = roleList;
        mRoleName = roleName;
        mRoleCode = roleCode;
        mGroupName = groupName;
        mGroupCode = groupCode;
        mInStorageFreeAddOperatorListener = inStorageFreeAddOperatorListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mOperatorList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mOperatorList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_free_add_operator_list_first_item, null);
            TextView purchase = (TextView) convertView.findViewById(R.id.purchase);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            TextView roleName = (TextView) convertView.findViewById(R.id.roleName);
            TextView roleCode = (TextView) convertView.findViewById(R.id.roleCode);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            purchase.setText(mPurchase);
            supplier.setText(mSupplier);
            roleName.setText(TextUtils.isEmpty(mRoleName) ? mContext.getResources().getString(R.string.activity_receive_add_receiver_choose_role) : mRoleName);
            roleCode.setText(TextUtils.isEmpty(mRoleCode) ? "" : mRoleCode);
            TextView groupName = (TextView) convertView.findViewById(R.id.groupName);
            TextView groupCode = (TextView) convertView.findViewById(R.id.groupCode);
            roleName.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    DialogUtils.getRoleaDialogNew(mContext, mRoleList, new DialogUtils.RoleListener() {
                        @Override
                        public void onSelect(Dialog dialog, String roleNameStr, String roleCodeStr) {
                            roleName.setText(roleNameStr);
                            roleCode.setText(roleCodeStr);
                            mInStorageFreeAddOperatorListener.onSelectRole(roleNameStr, roleCodeStr);
                        }
                    }).show();
                }
            });
            groupName.setText(TextUtils.isEmpty(mGroupName) ? mContext.getResources().getString(R.string.activity_receive_add_receiver_choose_user_group) : mGroupName);
            groupCode.setText(TextUtils.isEmpty(mGroupCode) ? "" : mGroupCode);
            groupName.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mInStorageFreeAddOperatorListener.onClickGroupNameListener();
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    String roleCodeStr = roleCode.getText().toString();
                    if (TextUtils.isEmpty(roleCodeStr)) {
                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_receive_add_receiver_choose_role));
                        return;
                    }
                    String conditionStr = condition.getText().toString();
                    if (TextUtils.isEmpty(conditionStr)) {
                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_receive_add_receiver_condition_null));
                        return;
                    }
                    Intent intent = new Intent(mContext, InStorageFreeSelectOperatorActivity.class);
                    intent.putExtra(InStorageExtra.IN_STORAGE_CONDITION, conditionStr);
                    intent.putExtra(InStorageExtra.IN_STORAGE_ROLE, roleCodeStr);
                    mContext.startActivity(intent);
                }
            });
        } else {
            GetOperatorBean.OperatorBean operatorItem = mOperatorList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_free_add_operator_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, operatorItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.id = (TextView) view.findViewById(R.id.id);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.roleName = (TextView) view.findViewById(R.id.roleName);
        viewHolder.department = (TextView) view.findViewById(R.id.department);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, GetOperatorBean.OperatorBean operatorItem) {
        viewHolder.id.setText(operatorItem.getUser_id());
        viewHolder.name.setText(operatorItem.getUser_name());
        viewHolder.roleName.setText(operatorItem.getRole_name());
        viewHolder.department.setText(operatorItem.getOrg_name());
        view.setOnLongClickListener((View v) -> {
            DialogUtils.getConfirmDialog(mContext, mContext.getResources().getString(R.string.receiver_list_adapter_confirm_delete) + operatorItem.getUser_name(), new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    InStorageUtils.deleteFreeOperator(mContext, operatorItem.getUser_id());
                    dialog.cancel();
                    mInStorageFreeAddOperatorListener.onDeleteOperatorListener();
                }
            }).show();
            return true;
        });
    }

    private class ViewHolder {
        private TextView id, name, roleName, department;
    }

}
